package com.hissy.common.generator_01;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

/**
 * Created by zhangyanfei on 2018/5/29.
 * 扩展ClassWriter
 *
 * @author zhangyanfei
 * @date 2018/05/29
 */
public class PocClassWriter extends ClassWriter {
    public PocClassWriter(int flags) {
        super(flags);
    }

    public PocClassWriter(ClassReader classReader, int flags) {
        super(classReader, flags);
    }

    /**
     * 判断2个类 谁是是父类
     *
     * @param var1
     * @param var2
     * @return
     */
    protected String getCommonSuperClass(String var1, String var2) {
        // 使用当前线程的内加载器
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        Class var4;
        Class var5;
        try {
            var4 = Class.forName(var1.replace('/', '.'), false, classLoader);
            var5 = Class.forName(var2.replace('/', '.'), false, classLoader);
        } catch (Exception var7) {
            throw new RuntimeException(var7.toString());
        }

        if (var4.isAssignableFrom(var5)) {
            return var1;
        } else if (var5.isAssignableFrom(var4)) {
            return var2;
        } else if (!var4.isInterface() && !var5.isInterface()) {
            do {
                var4 = var4.getSuperclass();
            } while (!var4.isAssignableFrom(var5));

            return var4.getName().replace('.', '/');
        } else {
            return "java/lang/Object";
        }
    }
}
